var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({
  data:{
    orderList:[],
    page:1,
    size: 10,
    loadmoreText:'正在加载更多数据',
    nomoreText:'我是有底线的',
    nomore:false,
    selectedAll:true,
    toBePaid:false,
    toBeShipped:false,
    toBeReceived:false,
    selectOrderStatus:"",
    totalPages:1
  },
  onLoad:function(options){
    wx.showLoading({
      title: '加载中...',
      success: function(){}
    });
    this.getOrderList()
  },
  selectedAllTab: function (event){
    this.setData({
      selectedAll: true,
      toBePaid: false,
      toBeShipped: false,
      toBeReceived: false,
      selectOrderStatus:event.target.dataset.type
    })
    this.getTabOrderList();
  },
  selectedPaidTab: function (event) {
    this.setData({
      selectedAll: false,
      toBePaid:true,
      toBeShipped: false,
      toBeReceived: false,
      selectOrderStatus: event.target.dataset.type
    })
    this.getTabOrderList();
  },
  selectedShippedTab: function (event) {
    this.setData({
      selectedAll: false,
      toBePaid: false,
      toBeShipped: true,
      toBeReceived: false,
      selectOrderStatus: event.target.dataset.type
    })
    this.getTabOrderList();
  },
  selectedReceivedTab: function (event) {
    this.setData({
      selectedAll: false,
      toBePaid: false,
      toBeShipped: false,
      toBeReceived: true,
      selectOrderStatus: event.target.dataset.type
    })
    this.getTabOrderList();
  },
  onReachBottom: function(){
    this.getOrderList()
  },
  onPullDownRefresh(){
    let that = this;
    util.request(api.OrderList, { page: 1, size: that.data.size, orderStatus: that.data.selectOrderStatus}).then(function (res){
      if (res.errno === 0){
        that.setData({
          orderList: that.data.orderList,
          totalPages:res.data.totalPages
        });
        wx.hideLoading();
      } else {
        util.showErrorToast(res.errmsg);
      }
    });
  },
  getOrderList(){
    let that = this;
    if (that.data.totalPages <= that.data.page - 1){
      that.setData({
        nomore:true
      })
      return;
    }
    util.request(api.OrderList, { page: that.data.page, size: that.data.size, orderStatus:that.data.selectOrderStatus}).then(function (res){
      if (res.errno === 0){
        that.setData({
          orderList:that.data.orderList.concat(res.data.data),
          page:res.data.currentPage+1,
          totalPages:res.data.totalPages
        });
        wx.hideLoading();
      }else{
        util.showErrorToast(res.errmsg);
      }
    });
  },
  getTabOrderList(){
    let that = this;
    util.request(api.OrderList, { page:1, size: that.data.size, orderStatus: that.data.selectOrderStatus }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          orderList: res.data.data,
          totalPages: res.data.totalPages
        });
        wx.hideLoading();
      } else {
        util.showErrorToast(res.errmsg);
      }
    });
  },
  payOrder(event){
      let that = this;
      let orderId = event.target.dataset.id;
    let orderPrice = event.target.dataset.type;
      wx.navigateTo({
        url: '/pages/pay/pay?orderId=' + orderId + '&actualPrice=' + orderPrice,
      })
  },
  cancelOrder(event) {
    let that = this;
    let orderId = event.target.dataset.id;
    let orderStatus = event.target.dataset.order_status;

    var errorMessage = '';
    switch (orderStatus) {
      case 300: {
        errorMessage = '订单已发货';
        break;
      }
      case 301: {
        errorMessage = '订单已收货';
        break;
      }
      case 101: {
        errorMessage = '订单已取消';
        break;
      }
      case 102: {
        errorMessage = '订单已删除';
        break;
      }
      case 401: {
        errorMessage = '订单已退款';
        break;
      }
      case 402: {
        errorMessage = '订单已退货';
        break;
      }
    }
    if (errorMessage != '') {
      util.showErrorToast(errorMessage);
      return false;
    }
    wx.showModal({
      title: '',
      content: '确定要取消此订单？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderCancel, {
            orderId:orderId
          }).then(function (res){
            if (res.errno === 0) {
              wx.showModal({
                title: '提示',
                content:res.data,
                showCancel: false,
                confirmText: '继续',
                success: function (res){
                  that.onLoad();
                }
              });
            }
          });
        }
      }
    });
  },
  confirmOrder(event){
    let that = this;
    let orderId = event.target.dataset.id;
    let orderStatus = event.target.dataset.order_status;
    var errorMessage = '';
    switch (orderStatus) {
      case 301: {
        errorMessage = '订单已收货';
        break;
      }
      case 101: {
        errorMessage = '订单已取消';
        break;
      }
      case 102: {
        errorMessage = '订单已删除';
        break;
      }
      case 401: {
        errorMessage = '订单已退款';
        break;
      }
      case 402: {
        errorMessage = '订单已退货';
        break;
      }
    }

    if (errorMessage != '') {
      util.showErrorToast(errorMessage);
      return false;
    }

    wx.showModal({
      title: '',
      content: '确定已经收到商品？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderConfirm, {
            orderId:orderId
          }).then(function (res){
            if (res.errno === 0){
              wx.showModal({
                title:'提示',
                content:res.data,
                showCancel: false,
                confirmText:'继续',
                success:function (res){
                  that.onLoad();
                }
              });
            }
          });
        }
      }
    });
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){

  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})